package com.dsbj.icardservice.service;

import com.dsbj.icardservice.model.WorkflowFieldInfo;
import com.dsbj.icardservice.webserviceclient.ecology.workflow.*;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class EcologyWebService{
    public WorkflowRequestInfo buildWorkflowRequest(String workflowId, String creatorId, String requestLevel, String requestName, List<WorkflowFieldInfo> mainDataList, List<List<WorkflowFieldInfo>> detailDataList) {


        //设置创建流程的ID
        WorkflowBaseInfo wbi = new WorkflowBaseInfo();
        wbi.setWorkflowId(workflowId);

        //设置主表的信息
        List<WorkflowRequestTableField> wrtiList = mainDataList.stream().map(x-> buildTableField(x)).collect(Collectors.toList());
        ArrayOfWorkflowRequestTableField wrtiArray = new ArrayOfWorkflowRequestTableField();
        wrtiArray.getWorkflowRequestTableField().addAll(wrtiList);
        ArrayOfWorkflowRequestTableRecord wrtriArray = new ArrayOfWorkflowRequestTableRecord();
        WorkflowRequestTableRecord wrtri = new WorkflowRequestTableRecord();
        wrtri.setWorkflowRequestTableFields(wrtiArray);
        wrtriArray.getWorkflowRequestTableRecord().add(wrtri);
        WorkflowMainTableInfo wmi = new WorkflowMainTableInfo();
        wmi.setRequestRecords(wrtriArray);

        //设置详细表信息
        ArrayOfWorkflowRequestTableRecord dWrtriArray = new ArrayOfWorkflowRequestTableRecord();
        dWrtriArray.getWorkflowRequestTableRecord().addAll(detailDataList.stream().map(x->{
            WorkflowRequestTableRecord record = new WorkflowRequestTableRecord();
            ArrayOfWorkflowRequestTableField dWrtiArray = new ArrayOfWorkflowRequestTableField();
            List<WorkflowRequestTableField> dWrtiList = x.stream().map(y-> buildTableField(y)).collect(Collectors.toList());
            dWrtiArray.getWorkflowRequestTableField().addAll(dWrtiList);
            record.setWorkflowRequestTableFields(dWrtiArray);
            return record;
        }).collect(Collectors.toList()));

        ArrayOfWorkflowDetailTableInfo detailTableInfoArray = new ArrayOfWorkflowDetailTableInfo();
        WorkflowDetailTableInfo detailTableInfo = new WorkflowDetailTableInfo();
        detailTableInfo.setWorkflowRequestTableRecords(dWrtriArray);
        detailTableInfoArray.getWorkflowDetailTableInfo().add(detailTableInfo);
        WorkflowRequestInfo wri = new WorkflowRequestInfo();
        wri.setCreatorId(creatorId);
        wri.setRequestLevel(requestLevel);
        wri.setRequestName(requestName);
        wri.setWorkflowMainTableInfo(wmi);
        wri.setWorkflowDetailTableInfos(detailTableInfoArray);
        wri.setWorkflowBaseInfo(wbi);

        return wri;
    }

    private WorkflowRequestTableField buildTableField(WorkflowFieldInfo workflowFieldInfo) {
        WorkflowRequestTableField wrti = new WorkflowRequestTableField();
        wrti.setFieldName(workflowFieldInfo.fieldName());//
        wrti.setFieldValue(workflowFieldInfo.fieldValue());//
        wrti.setView(workflowFieldInfo.canView());//字段是否可见
        wrti.setEdit(workflowFieldInfo.canEdit());//字段是否可编辑
        return wrti;
    }
}
